﻿namespace AlgorithmInterview.Chapter01._02;

/// <summary>
/// 方法二：递归方法
/// </summary>
public class Test02
{
    public static LNode RemoveDupRecursion(LNode head)
    {
        if (head.next == null)
            return head;
        LNode pointer = null;
        LNode cur = head;
        // 对已head.next为首的子链表删除重复的结点
        head.next = RemoveDupRecursion(head.next);
        pointer = head.next;
        // 找出以head.next为首的子链表中与head结点相同的结点并删除
        while (pointer != null)
        {
            if (head.data == pointer.data)
            {
                cur.next = pointer.next;
                pointer = cur.next;
            }
            else
            {
                pointer = pointer.next;
                cur = cur.next;
            }
        }

        return head;
    }

    /// <summary>
    /// 对带头结点的单链表删除重复结点
    /// </summary>
    /// <param name="head">指向链表头结点</param>
    public static void RemoveDup(LNode head)
    {
        if (head == null)
            return;
        head.next = RemoveDupRecursion(head);
    }
}